﻿app.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function () {
                scope.$apply(function () {
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);

app.directive('gridloader', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        template: '<div class="col-md-12 griddiv" ng-show="showgridloader"><div class="col-md-12 errordiv"><div id="cssload-cupcake" class="cssload-box"><span class="cssload-letter">L</span><div class="cssload-cupcakeCircle cssload-box"><div class="cssload-cupcakeInner cssload-box"><div class="cssload-cupcakeCore cssload-box"></div></div></div><span class="cssload-letter cssload-box">A</span><span class="cssload-letter cssload-box">D</span><span class="cssload-letter cssload-box">I</span><span class="cssload-letter cssload-box">N</span><span class="cssload-letter cssload-box">G</span></div></div><div></div><div></div><div></div></div></div></div>',
        link: function (scope, element, attrs) {
        }
    };
}]);

app.directive('nodata', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        template: '<div class="col-md-12" ng-show="list.length == 0" ><div class="col-md-12 errordiv"><h1><i class="zmdi zmdi-alert-octagon erroricon"></i></h1><h4>No Data</h4></div></div>',
        link: function (scope, element, attrs) {
        }
    };
}]);

app.directive('actualImage', ['$timeout', function ($timeout) {
    return {
        link: function ($scope, element, attrs) {
            function waitForImage(url) {
                var tempImg = new Image();
                tempImg.onload = function () {
                    $timeout(function () {
                        element.attr('src', url);
                    });
                }
                tempImg.src = url;
            }

            attrs.$observe('actualImage', function (newValue) {
                if (newValue) {
                    waitForImage(newValue);
                }
            });
        }
    }
}]);

app.directive('onErrorSrc', function () {
    return {
        link: function (scope, element, attrs) {
            element.bind('error', function () {
                if (attrs.src != attrs.onErrorSrc) {
                    attrs.$set('src', attrs.onErrorSrc);
                }
            });
        }
    }
});

app.directive('onErrorSrc', function () {
    return {
        link: function (scope, element, attrs) {
            element.bind('error', function () {
                if (attrs.src != attrs.onErrorSrc) {
                    attrs.$set('src', attrs.onErrorSrc);
                }
            });
        }
    }
});

app.directive('calendar', function ($rootScope) {
    return {
        require: 'ngModel',
        link: function (scope, el, attr, ngModel) {
            $(el).datepicker({
                dateFormat: $rootScope.dateformet,
                onSelect: function (dateText) {
                    scope.$apply(function () {
                        ngModel.$setViewValue(dateText);
                    });
                },
                changeMonth: true,
                changeYear: true
            });
        }
    };
})

